package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderRefundStateDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 退货状态展示adapter
 */
public class OrderAfterSaleStateAdapter extends BaseRecyclerViewAdapter<OrderRefundStateDataBean.
        DataBean.ProcessBean> {

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    public OrderAfterSaleStateAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_order_after_sale_state, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderRefundStateDataBean.DataBean.ProcessBean stateBean = mList.get(position);
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).setData(stateBean);
            ((ViewHolder) holder).setListener(stateBean);
        }
        //第一个item设置隐藏线，显示帮助
        if (position == 0) {
            ((ViewHolder)holder).viewItemTopLine.setVisibility(View.INVISIBLE);
            ((ViewHolder)holder).tvRefundHelp.setVisibility(View.VISIBLE);
        }else{
            ((ViewHolder)holder).viewItemTopLine.setVisibility(View.VISIBLE);
            ((ViewHolder)holder).tvRefundHelp.setVisibility(View.INVISIBLE);
        }

    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        /**
         * item左面贯穿线上半部分
         */
        @BindView(R.id.view_item_top_line)
        View viewItemTopLine;

        /**
         * 退款状态
         */
        @BindView(R.id.tv_refund_state)
        TextView tvRefundState;

        /**
         * 退款帮助
         */
        @BindView(R.id.tv_refund_help)
        TextView tvRefundHelp;

        /**
         * 状态说明
         */
        @BindView(R.id.tv_refund_explain)
        TextView tvRefundExplain;

        /**
         * 状态时间
         */
        @BindView(R.id.tv_refund_time)
        TextView tvRefundTime;

        /**
         * item左侧小点
         */
        @BindView(R.id.image_left)
        QMUIRadiusImageView imageLeft;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setData(OrderRefundStateDataBean.DataBean.ProcessBean bean) {

            tvRefundState.setText(bean.getStatus());
            tvRefundExplain.setText(bean.getStatusdef());

            //判断是否存在状态
            if (bean.isTree()) {
                tvRefundTime.setText(bean.getTime());
                imageLeft.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_oval_blue));
                tvRefundState.setTextColor(ContextCompat.getColor(mContext,R.color.blue_67a0ff));
            } else {
                tvRefundTime.setVisibility(View.INVISIBLE);
                imageLeft.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_oval_gray));
                tvRefundState.setTextColor(ContextCompat.getColor(mContext,R.color.black));
            }
        }

        private void setListener(final OrderRefundStateDataBean.DataBean.ProcessBean bean) {
            /**
             * 帮助按钮
             */
            tvRefundHelp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        //接口实例化后的而对象，调用重写后的方法
                        mOnItemClickListener.onHelpClickListener();
                    }
                }
            });

        }
    }
    public interface OnItemClickListener {
        void onHelpClickListener();
    }
    public void setOnClickListener(OrderAfterSaleStateAdapter.OnItemClickListener onHelpClickListener) {
        this.mOnItemClickListener = onHelpClickListener;
    }
}
